package com.itguigu.rabbitmq.three;

import com.itguigu.rabbitmq.utils.RabbitMqutil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Date 2021/7/18 13:40
 * @Created by wangle
 * @Description 消息在手动应答时不丢失，放回对列中重新消费
 */
public class Work04 {
    //队列名称
    public static String task_queue_name = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqutil.getChannel();
        System.out.println("c2等待消息处理等待时间较长");


        //声明 接受消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {

            //沉睡1s
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("接收到的消息：" + new String(message.getBody()));

            /*手动应答
            1.消息的标记
            2.是否批量应答
            * */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);


        };

        //取消消息时的回调
        CancelCallback cancelCallback = (consumerTag) -> {

            System.out.println("消息消费被中断");
        };

        //设置不公平分发
        channel.basicQos(5);

        //采用手动应答
        boolean auto_ack = false;
        channel.basicConsume(task_queue_name, auto_ack, deliverCallback, cancelCallback);

    }
}
